﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace UnrealConverterLogic
{
    public enum Units
    {
        UNREAL_UNITS,
        MAX_UNITS,
        METERS
    }
    public partial class UnrealConverter
    {
        private static Form formMain;
        private static TextBox tbInputSetName;
        private static TextBox tbInputUU;
        private static TextBox tbInputMaxUnits;
        private static TextBox tbInputMeters;
        private static TextBox tbUURatio;
        private static TextBox tbMaxRatio;
        private static TextBox tbMeterRatio;
        private static TextBox tbProject;
        private static GroupBox gBInputSetName;
        private static GroupBox gBUU;
        private static GroupBox gBMaxUnits;
        private static GroupBox gBMeters;
        private static ComboBox cbRatios;
        private static Label lDynamicScaleIncrease;
        private static Label lRatio;
        private static DataGridView dgvReferences;
        private static ListBox lvRatioList;
        private static RadioButton rbRatios;
        private static RadioButton rbConversion;
        private static bool bClosing = false;
        public static bool bFormRatiosOpen = false;
        public static bool bFormGridsOpen = false;
        public static bool bFormReferencesOpen = false;
        public static Point formMainPosition;
        public static Size formMainSize;
        private static int iHeightBeforeResize = 700;
        private static int iWidthBeforeResize = 700;
        private static bool isUUMathExpression = false;
        private static bool isMaxMathExpression = false;
        private static bool isMetersMathExpression = false;
        private static double fUnrealUnits;
        private static double fMaxUnits;
        private static double fMeters;
        private static double fMaxtoUU;
        private static double fMaxtoMeters;
        private static double fUUtoMeters;
        private static double fUUtoMax;
        private static double fMeterstoUU;
        private static double fMeterstoMax;
        private static bool bEditingRatios = false;
        private static bool bConverting = true;
        private enum InputField
        {
            UNREAL,
            MAX,
            METERS
        };
        private static InputField LastFocusedInputField;
        public static void AddFormReference(Form f)
        {
            if (f.Name == "formMainConverter")
            {
                formMain = f;
                tbInputSetName = (TextBox)formMain.Controls.Find("tbInputSetName", true).First();
                tbInputUU = (TextBox)formMain.Controls.Find("tbInputUU", true).First();
                tbInputMaxUnits = (TextBox)formMain.Controls.Find("tbInputMaxUnits", true).First();
                tbInputMeters = (TextBox)formMain.Controls.Find("tbInputMeters", true).First();
                gBInputSetName = (GroupBox)formMain.Controls.Find("gBInputSetName", true).First();
                gBUU = (GroupBox)formMain.Controls.Find("gBUU", true).First();
                gBMaxUnits = (GroupBox)formMain.Controls.Find("gBMaxUnits", true).First();
                gBMeters = (GroupBox)formMain.Controls.Find("gBMeters", true).First();
                rbRatios = (RadioButton)formMain.Controls.Find("radioButton2", true).First();
                rbConversion = (RadioButton)formMain.Controls.Find("radioButton1", true).First();
                lDynamicScaleIncrease = (Label)formMain.Controls.Find("lDynamicScaleIncrease", true).First();
                lRatio = (Label)formMain.Controls.Find("lRatio", true).First();
                dgvReferences = (DataGridView)formMain.Controls.Find("dgvReferences", true).First();
                lvRatioList = (ListBox)formMain.Controls.Find("gbRatios", true).First().Controls.Find("listBox1", true).First();

                tbInputSetName.KeyDown += UnrealConverter.CatchHotKeys;
                tbInputUU.KeyPress += UnrealConverter.Validate;
                tbInputUU.KeyDown += UnrealConverter.CatchHotKeys;
 
                tbInputMaxUnits.KeyPress += UnrealConverter.Validate;
                tbInputMaxUnits.KeyDown += UnrealConverter.CatchHotKeys;

                tbInputMeters.KeyPress += UnrealConverter.Validate;
                tbInputMeters.KeyDown += UnrealConverter.CatchHotKeys;

                dgvReferences.DoubleClick += UnrealConverter.OnRowDoubleClicked;
                dgvReferences.KeyDown += UnrealConverter.OnDataGridViewDeletePressed;

                lvRatioList.KeyDown += UnrealConverter.OnRatioListDeletePressed;
                lvRatioList.MouseDown += UnrealConverter.OnRatioListItemSelected;

                rbRatios.CheckedChanged += UnrealConverter.SwitchMainInputsToRatios;
                rbConversion.CheckedChanged += UnrealConverter.SwitchMainInputsToConversion;
                SetGridArrays();
            }
        }
        public static void SetRefFormSize(Size s)
        {
            iHeightBeforeResize = s.Height;
            iWidthBeforeResize = s.Width;
        }
        public static void InitDataContainers()
        {
            references = new List<Reference>();
            Ratios = new List<Ratio>();
            UsedRatio = new Ratio("Default", 1, 1, 0.01);
        }
    }
}
